package com.mdd.maduoduo.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mdd.maduoduo.R;
import com.mdd.maduoduo.adapter.MaxDataAdapter;
import com.mdd.maduoduo.constants.CommonConstants;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.MaxDataEntity;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.Utils;
import com.mdd.maduoduo.view.interfaces.GetMaxView;

import java.util.List;

import javax.inject.Inject;

public class MaxActivity extends BaseActivity implements GetMaxView {

    private TextView tvBtnLeftOne,tvBtnLeftThree,tvBtnLeftTwo,tvLeftBottom,tvRightOne,tvRightTwo,tvRightThree,tvRightBottom;

    private LinearLayout llBtnLeft,llBtnRight;

    @Inject
    public LoginPresenter mPresenter;

    private ImageView ivBack;

    private RecyclerView recyclerView;

    private MaxDataAdapter mAdapter;

    private TextView tvSologo;

    private TextView tvExchange;

    private TextView tvPrivate,tvServiceRule;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_max;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setGetMaxView(this);
    }

    @Override
    protected void initView() {
        super.initView();
        tvBtnLeftOne = findViewById(R.id.tvBtnLeftOne);
        tvBtnLeftTwo = findViewById(R.id.tvBtnLeftTwo);
        tvBtnLeftThree = findViewById(R.id.tvBtnLeftThree);
        tvLeftBottom = findViewById(R.id.tvLeftBottom);
        tvRightOne = findViewById(R.id.tvRightOne);
        tvRightTwo = findViewById(R.id.tvRightTwo);
        tvRightThree = findViewById(R.id.tvRightThree);
        tvRightBottom = findViewById(R.id.tvRightBottom);
        llBtnLeft = findViewById(R.id.llBtnLeft);
        llBtnRight = findViewById(R.id.llBtnRight);
        ivBack = findViewById(R.id.ivBack);
        tvSologo = findViewById(R.id.tvSologo);
        recyclerView = findViewById(R.id.recyclerView);
        tvExchange = findViewById(R.id.tvExchange);
        tvPrivate = findViewById(R.id.tvPrivate);
        tvServiceRule = findViewById(R.id.tvServiceRule);
        mAdapter = new MaxDataAdapter();
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setNestedScrollingEnabled(false);
        mPresenter.getMaxData();

        //立即兑换
        String exchange = "有兑换码? 请立即兑换";
        SpannableString spannableString = new SpannableString(exchange);

        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                //跳转兑换页面
                startActivity(new Intent(MaxActivity.this,ExchangeActivity.class));
            }
        }, 7, exchange.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);


        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#007AFF")),
                7, exchange.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvExchange.setText(spannableString);
        tvExchange.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    protected void onClick() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvPrivate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MaxActivity.this,WebViewActivity.class);
                intent.putExtra("title","隐私政策");
                intent.putExtra("url", CommonConstants.Privacy);
                startActivity(intent);
            }
        });

        tvServiceRule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MaxActivity.this,WebViewActivity.class);
                intent.putExtra("title","用户协议");
                intent.putExtra("url",CommonConstants.USER_RULE);
                startActivity(intent);
            }
        });
    }

    @Override
    public void renderSuccess(MaxDataEntity maxDataEntity) {
        tvSologo.setText(maxDataEntity.data.sologo);
        //Max数据展示
        mAdapter.setData(maxDataEntity.data.normalVsMaxDtos);
        //底部按钮数据
        List<MaxDataEntity.DataDTO.PayButtonDtosDTO> list  = maxDataEntity.data.payButtonDtos;
        if (list.size()==1){
            llBtnLeft.setVisibility(View.VISIBLE);
            tvBtnLeftOne.setText(list.get(0).content1);
            tvBtnLeftTwo.setText(list.get(0).content2);
            tvBtnLeftThree.setText(list.get(0).content3);
            tvLeftBottom.setText(list.get(0).content4);

            llBtnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (list.get(0).jumpType==0){
                        Utils.startWeb(MaxActivity.this,list.get(0).iosProductId);
                    }
                }
            });

        }else {
            llBtnLeft.setVisibility(View.VISIBLE);
            llBtnRight.setVisibility(View.VISIBLE);
            tvBtnLeftOne.setText(list.get(0).content1);
            tvBtnLeftTwo.setText(list.get(0).content2);
            tvBtnLeftThree.setText(list.get(0).content3);
            tvLeftBottom.setText(list.get(0).content4);

            tvRightOne.setText(list.get(1).content1);
            tvRightTwo.setText(list.get(1).content2);
            tvRightThree.setText(list.get(1).content3);
            tvRightBottom.setText(list.get(1).content4);

            llBtnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (list.get(0).jumpType==0){
                        Utils.startWeb(MaxActivity.this,list.get(0).iosProductId);
                    }
                }
            });

            llBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (list.get(1).jumpType==0){
                        Utils.startWeb(MaxActivity.this,list.get(1).iosProductId);
                    }
                }
            });
        }
    }
}
